/**
 *@Title: IncomingController.java  
* @author: 胡彪
* @date: 2013-6-1 下午02:51:05
* @Description:
* @version: V1.0   
 */
package com.imonsoft.sqm.web.business.ipic_system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.business.ipic_system.Incoming;
import com.imonsoft.sqm.service.business.ipic_system.IncomingService;
import com.imonsoft.sqm.vo.business.ipic_system.IncomingVo;

/**
 *
 *
 */
@Controller
@RequestMapping("/business/ipic_system/incoming")
public class IncomingController {
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private IncomingService incomingService;
	
	@RequestMapping("/listUI.do")
	public void listUI(ModelMap model) {
	}
	
	
	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, Incoming incoming) {
		DataGrid<IncomingVo> dataGrid = new DataGrid<IncomingVo>();
		try {
			dataGrid = incomingService.getDataGridVoBy(incoming, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}
	
	@RequestMapping(value = "/addUI.do")
	public void addUI(ModelMap model) {

	}
	
	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, Incoming bean,BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			String incoming = request.getParameter("incoming"); // 获取总目
			String incomingDetail = request.getParameter("incomingDetail"); // 获取细目
			incomingService.txAddIncoming(incoming, incomingDetail,message,result);
			
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long sid,Long pid, String year) {

		if(sid==null||pid==null||year==null){
			new Exception(CCommon.EXCEPTION_MSG);
		} 
	 
		IncomingVo vo=incomingService.getModifyData(sid, pid, year);
		model.put("incomingVo", vo); 
	}
	
	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			
			String incoming = request.getParameter("incoming"); // 获取细目
			String incomingDetail = request.getParameter("incomingDetail"); // 获取总目
			incomingService.txUpdateOrAddIncoming(incoming, incomingDetail);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response) {
		ReturnMessage message = new ReturnMessage();
		try {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			String incomingDetail = request.getParameter("incomingDetail"); // 获取总目
			incomingService.deleteIncoming(incomingDetail);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
}
